$(function (){
	var thisURL = document.URL;    
	var getval =thisURL.split('?')[1];  
	var voteid= getval.split("=")[1];
	$.ajax({
    	type: "POST",
    	url: "/VOTE/vote/get",
    	data:{
    		id:voteid
    	},
    	cache : false,  //禁用缓存
        dataType: "json",
    	success: function(res){
    		var obj = $("#title");
    		var str="<label><p>"+res.title+"</p><p>"+unixToDate(res.startdt,'Y-m-d',24)+"到"+unixToDate(res.enddt,'Y-m-d',24)+"</p></label>";
    		obj.append(str);
        }
	});
	
	
	initDetail();
	
	function initDetail(){
		$.ajax({
	    	type: "POST",
	    	url: "/VOTE/getDetails",
	    	data:{
	    		voteid:voteid
	    	},
	    	cache : false,  //禁用缓存
	        dataType: "json",
	    	success: function(res){
	    		console.debug(res);
	    		var form = $("#vote");
	    		form.children('tbody').remove(); 
	    		if(res.length>0){
	    			for(var i=0;i<res.length;i++){
	    				form.append(getHtmlStr(res[i].id,res[i].title,res[i].qty));
	        		}
	    		}else{
	    			form.append("<h1>没有维护投票项！！</h1>");
	    		}
	        }
		});
	}
	/**              
     * 时间戳转换日期              
     * @param <int> unixTime    待时间戳(秒)              
     * @param <bool> isFull    返回完整时间(Y-m-d 或者 Y-m-d H:i:s)              
     * @param <int>  timeZone   时区              
     */
    function unixToDate(unixTime, isFull, timeZone) {
        if (typeof (timeZone) == 'number')
        {
            unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
        }
        var time = new Date(unixTime);
        var ymdhis = "";
        ymdhis += time.getUTCFullYear() + "-";
        ymdhis += (time.getUTCMonth()+1) + "-";
        ymdhis += time.getUTCDate();
        if (isFull === true)
        {
            ymdhis += " " + time.getUTCHours() + ":";
            ymdhis += time.getUTCMinutes() + ":";
            ymdhis += time.getUTCSeconds();
        }
        return ymdhis;
    }
	function getdescript(id){
		alert(id);
		window.location.href="/VOTE/descipt?id="+id;
	}
	function getHtmlStr(id,title,qty){
		if(qty==null){
			qty=0;
		}
		var html="<tr>"+
    				"<td class='td-raido'><input type='radio' name='id' value='"+id+"'></td>"+
    				"<td class='td-title'><label>"+title+"</label></td>"+
    				"<td class='td-label'><label>"+qty+"</label></td>"+
    				"<td class='td-label'><a type='button' class='btn btn-success' href='/VOTE/descipt?id="+id+"'>详细情况</a></td>"+
    			"</tr>";
		return html;
	}
	$('#voteform').form({
    	url:"/VOTE/setvote",
    	onSubmit:function(){
    		return $(this).form('validate');
    	},
    	success: function (res) { 
    		res=eval('(' + res + ')');
    		if(res.status=='success'){
    			$.messager.alert("提示完成","投票成功","success");
    			initDetail();
    		}else{
    			$.messager.alert("提示信息","投票失败","warning");
    			return;
    		}
    	}
    });
	
});